package com.changgou.common.advice;

import com.changgou.common.entity.HttpRespond;
import com.changgou.common.enums.HttpStateEnum;
import com.changgou.common.exception.ChangGouException;
import com.fasterxml.jackson.databind.JsonMappingException;
import feign.FeignException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.json.JsonParseException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.nio.file.AccessDeniedException;
import java.util.List;

/**
 * 统一异常处理Handler
 */
@RestControllerAdvice
public class CommonExceptionHandler {

    private final static Logger log = LoggerFactory.getLogger(CommonExceptionHandler.class);

    @ExceptionHandler(value = NullPointerException.class)
    public HttpRespond<Void> exceptionHandler(NullPointerException e){
        log.error("Null pointer exception: " + e.toString());
        return new HttpRespond<>(500, e.getMessage());
    }

    @ExceptionHandler(value = ChangGouException.class)
    public HttpRespond<Void> internShipException(ChangGouException e){
        return new HttpRespond<>(e.getHttpStateEnum());
    }

    /** validation异常捕获处理 **/
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public HttpRespond<Void> methodArgumentNotValidException(MethodArgumentNotValidException e){
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        return new HttpRespond<>(400, allErrors.get(0).getDefaultMessage());
    }

    @ExceptionHandler(BindException.class)
    public HttpRespond<Void> bindException(BindException e){
        List<ObjectError> allErrors = e.getAllErrors();
        return new HttpRespond<>(400, allErrors.get(0).getDefaultMessage());
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public HttpRespond<Void> handleBindException(ConstraintViolationException e) {
        return new HttpRespond<>(400, e.getMessage());
    }
    /** validation异常捕获处理 **/

    /** 权限校验异常处理 **/
    @ExceptionHandler(AccessDeniedException.class)
    public HttpRespond<Void> accessDeniedException(AccessDeniedException e){
        log.error("Insufficient permission: " + e.getMessage());
        return new HttpRespond<>(HttpStateEnum.UNAUTHORIZED);
    }
    /** 权限校验异常处理 **/

    /** 远程调用服务异常 **/
    @ExceptionHandler(FeignException.class)
    public HttpRespond<Void> feignException(FeignException e){
        log.error("Feign Remote invocation service exception: " + e.getMessage());
        return new HttpRespond<>(HttpStateEnum.UNKNOWN_EXCEPTION);
    }
    /** 远程调用服务异常 **/

    /** 前端json格式有误**/
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public HttpRespond<Void> httpMessageNotReadableExceptionHandler(HttpMessageNotReadableException e){
        log.error("Json format error: " + e.getMessage());
        return new HttpRespond<>(400, e.getMessage());
    }

    @ExceptionHandler(value = JsonParseException.class)
    public HttpRespond<Void> jsonParseExceptionHandler(JsonParseException e){
        log.error("Json parse format error: " + e.getMessage());
        return new HttpRespond<>(400, e.getMessage());
    }

    @ExceptionHandler(value = JsonMappingException.class)
    public HttpRespond<Void> jsonMappingExceptionHandler(JsonMappingException e){
        log.error("Json format error for mapping: " + e.getMessage());
        return new HttpRespond<>(400, e.getMessage());
    }
    /** 前端json格式有误**/

    @ExceptionHandler(value = Exception.class)
    public HttpRespond<Void> exceptionHandler(Exception e){
        log.error("Unknown error: " + e.getMessage());
        return HttpRespond.error();
    }

}
